package com.course.finalproject.service;

import java.util.ArrayList;
import java.util.List;

import android.app.IntentService;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;

import com.course.finalproject.Constants;
import com.course.finalproject.model.Weather;

public class WeatherService extends IntentService {

	/**
	 * Constructor.
	 */
	public WeatherService() {
		super("WeatherService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// Extracts data from intent.
		double lat = intent.getDoubleExtra(Constants.WEATHER_LAT_KEY, 0.0);
		double lon = intent.getDoubleExtra(Constants.WEATHER_LON_KEY, 0.0);
		int daysCount = intent.getIntExtra(Constants.WEATHER_DAYS_COUNT, 3);
		// Do the request of weathers.
		WeatherRequest request = new WundergroundWeatherRequest();
		List<Weather> weathers = request.requestWeather(lat, lon, daysCount);
		// Make the response intent and broadcast it.
		Intent broadcastIntent = new Intent(Constants.NEW_WEATHER_UPDATE);
		broadcastIntent.putExtra(Constants.WEATHER_FRESH_KEY, true);
		broadcastIntent.putParcelableArrayListExtra(Constants.WEATHER_LIST_PARCEL_KEY, (ArrayList<? extends Parcelable>) weathers);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
	}

}
